package factorytest.iflytek.com.einkiflytekfactorytest;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.DebugLog;

/**
 * Created by sst on 2018/4/29.
 */

public class ViewDialogFragment extends DialogFragment {

    private final String KEY_MESSAGE = "message";
    private final String KEY_TITLE = "title";


    public interface Callback {
        void onClick();
    }

    private Callback cancelCb;
    private Callback okCb;

    private String title = "";
    private String msg = "";

    private boolean isCancelable = true;

    public static ViewDialogFragment newInstance(String title, String msg, boolean cancelable) {
        ViewDialogFragment instance = new ViewDialogFragment();
        instance.setCancelable(cancelable);

        instance.setMessage(msg);
        instance.setTitle(title);

        return instance;
    }

    public static ViewDialogFragment newInstance(String title, String msg) {
        ViewDialogFragment instance = new ViewDialogFragment();
        instance.setCancelable(true);

        instance.setMessage(msg);
        instance.setTitle(title);

        return instance;
    }

    public void show(FragmentManager fragmentManager) {
        show(fragmentManager, null, null);
    }

    public void show(FragmentManager fragmentManager, Callback okCb, Callback cancelCb) {
        show(fragmentManager, "ViewDialogFragment");
        this.cancelCb = cancelCb;
        this.okCb = okCb;
    }

    public boolean isShowing() {
        return isAdded() || isVisible();
    }

    @Override
    public void onStart() {
        super.onStart();

        AlertDialog dialog = (AlertDialog) getDialog();
        dialog.setCancelable(isCancelable);

        Button btnPositive =
                dialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE);
        Button btnNegative =
                dialog.getButton(android.app.AlertDialog.BUTTON_NEGATIVE);

        if (btnPositive != null) {

            btnPositive.setBackgroundResource(R.drawable.selector_rect_background);

        }
        if (btnNegative != null) {

            btnNegative.setBackgroundResource(R.drawable.selector_rect_background);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialog);
        builder.setTitle(getTitle());
        builder.setMessage(getMessage());

        if (okCb != null) {
            builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (okCb != null) {
                        okCb.onClick();
                    }
                    dialog.dismiss();
                }
            });
        }

        if (cancelCb != null) {
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (cancelCb != null) {
                        cancelCb.onClick();
                    }
                    dialog.dismiss();
                }
            });
        }

        return builder.create();
    }



    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }

    public void setMessage(String msg) {
        doSetMessage(msg);
    }

    public void setTitle(String title) {
        doSetTitle(title);
    }

    private void doSetMessage(String msg) {
        Bundle bundle = getArguments();

        if (bundle == null) {
            bundle = new Bundle();
            setArguments(bundle);
        }

        bundle.putString(KEY_MESSAGE, msg);

    }

    private void doSetTitle(String title) {
        Bundle bundle = getArguments();

        if (bundle == null) {
            bundle = new Bundle();
            setArguments(bundle);
        }

        bundle.putString(KEY_TITLE, title);
    }

    public String getMessage() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return "";
        }
        return bundle.getString(KEY_MESSAGE);
    }

    public String getTitle() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return "";
        }
        return bundle.getString(KEY_TITLE);
    }
}
